package ces.frame.module.homePage.action;

import java.util.HashMap;

/**
 * 下午4:05:47
 * @author zhaoqun
 *
 */

public class Test {

	public static void count(String str){
        //将字符串转化为字符数组
        char[] chars = str.toCharArray();
        //创建一个HashMap名为hm
        HashMap<Character,Integer> hm = new HashMap();

        //定义一个字符串c，循环遍历遍历chars数组
        for(char c : chars){
            //containsKey(c),当c不存在于hm中
            if(!hm.containsKey(c)){
            hm.put(c,1);
          }else{ 
          //否则获得c的值并且加1
            hm.put(c, hm.get(c)+1);
            }

        //或者上面的if和else替换成下面这一行
        /*  hm.put(c,hm.containsKey(c) ? hm.get(c)+1:1);*/
        }


        for(Character key: hm.keySet()){
            //hm.keySet()代表所有键的集合,进行格式化输出
            System.out.println(key + "====" + hm.get(key));
        }
    }

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        String str = "abc,aaa,efdabb,hg";
        long beg = System.currentTimeMillis();
        count(str);
        long between = System.currentTimeMillis() - beg;
        System.out.println("用时【"+between+"】毫秒查询");
    }

}


